﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class AddBookType_form : Form
    {
        public AddBookType_form()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string booktype = textBox1.Text.Trim();


            if(booktype == "")
            {
                MessageBox.Show("空的书籍类型");
                textBox1.Focus();
                return;
            }
            if (DBhelper.checkBooktype(booktype))
            {
                MessageBox.Show("该类型已存在");
                textBox1.Focus();
                return;
            }

            DialogResult rt = MessageBox.Show($"确认添加：{booktype}吗？", "提示", MessageBoxButtons.YesNo);
            if(rt == DialogResult.Yes)
            {
                string sql = $"insert into BookType values('{booktype}')";
                if(DBhelper.NonQuery(sql))
                {
                    MessageBox.Show("添加成功");
                    ShowAllBookType();
                    textBox1.Clear();
                }
                else
                {
                    MessageBox.Show("添加失败");
                    textBox1.Focus();
                }
            }
        }

        private void AddBookType_form_Load(object sender, EventArgs e)
        {
            ShowAllBookType();
        }

        private void ShowAllBookType()
        {
            string sql = "select * from BookType";
            DataTable dt = DBhelper.GetDataTable(sql);
            listBox1.DataSource = dt;
            listBox1.DisplayMember = "typename";
        }
    }
}
